﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlatformManager : MonoBehaviour {

	public static PlatformManager Instance;			//Singleton 模式

	[Range(0f, 5f)]
	public float generateRange = 3.0f;				//平台產生的左右範圍
	public float distancePerPlatform = 3.06f;		//每個平台距離多遠
	public float baseVelocity = 1.3f;				//平台在Level=0時的速度
	public float velocityPerLevel = 0.3f;			//每提升一個Level增加多少速度

	public Platform platformPrefab;					//平台的Prefab

	private Queue<Platform> _platformPool = null;			//預產生物件池
	private List<Platform> _platformManageList = null;		//管理所有平台的Reference

	public Platform NextPlatform {					//從物件池取出還沒使用的平台，如果物件池是空的就再建立一個新的
		get {
			Platform platform = null;
			if (this._platformPool.Count > 0) {
				platform = this._platformPool.Dequeue();
			} else {
				platform = CreatPlatform(Vector2.zero, Quaternion.identity);
				this._platformManageList.Add(platform);
			}
			platform.velocity = Vector2.zero;
			platform.gameObject.SetActive(true);
			return platform;
		}
	}

	void Awake() {
		Instance = this;

		//建立物件池
		this._platformPool = new Queue<Platform>(10);
		this._platformManageList = new List<Platform>(10);
		for (int i = 0; i < 10; i++) {
			Platform platform = CreatPlatform(Vector2.zero, Quaternion.identity);
			platform.gameObject.SetActive(false);
			this._platformPool.Enqueue(platform);
			this._platformManageList.Add(platform);
		}
	}

	void Start() {

	}

	void OnEnable() {
		//開始製造平台
		StartCoroutine(PlatformGeneratorCoroutine());
	}

	void OnDisable() {
		StopAllCoroutines();
	}

	private IEnumerator PlatformGeneratorCoroutine() {
		while (this.enabled) {
			Vector2 pos = new Vector2(Random.Range(-generateRange, generateRange), this.transform.localPosition.y);
			Platform platform = this.NextPlatform;
			platform.position = pos;
			platform.velocity = new Vector2(0f, this.baseVelocity + GameManager.Instance.nowLevel * this.velocityPerLevel);
			//計算不同速度時要產生相同間距平台，要隔多久時間
			float nextTimeGap = this.distancePerPlatform / platform.velocity.y;
			yield return new WaitForSeconds(nextTimeGap);
		}
	}

	private Platform CreatPlatform(Vector2 pos, Quaternion rotation) {
		//產生一個新平台
		Platform platform = Object.Instantiate(
				this.platformPrefab, pos, rotation) as Platform;
		return platform;
	}

	public void RecyclePlatform(Platform platform) {
		//回收平台至物件池
		platform.gameObject.SetActive(false);
		this._platformPool.Enqueue(platform);
	}

	public void ClearAllPlatform() {
		//清除場上所有的平台
		Platform[] platforms = GameObject.FindObjectsOfType<Platform>();
		foreach (Platform p in platforms) {
			if (p.gameObject.activeSelf)
				RecyclePlatform(p);
		}
	}

	public void UpdateAllPlatformSpeed(int nowLevel) {
		//依照Level，更新所有場上平台的速度
		foreach (Platform platform in this._platformManageList) {
			if (platform.gameObject.activeSelf)
				platform.velocity = new Vector2(0f, this.baseVelocity + nowLevel * this.velocityPerLevel);
		}
	}
}
